import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author wufei
 * @date 2018/3/15/015 12:14
 */
public class Test1 {

    private volatile static int flag = 0;

    public static void main(String[] args) {
        Test1 t = new Test1();
        ExecutorService service = Executors.newFixedThreadPool(2);
        service.execute(()->{
            synchronized (t){
                for (int i = 0; i < 100; i++) {
                    if (i % 2 == 1) {
                        try {
                            if (flag == 1) {
                                t.wait();
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(i + "");
                    }
                }
                flag = 1;
                t.notify();
            }
        });

        service.execute(()->{

            synchronized (t){

                for (int i = 0; i < 100; i++) {
                    if(i%2 == 0) {
                        try {
                            if (flag == 0) {
                                t.wait();
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                        System.out.println(i+"");
                }
                flag = 0;
                t.notify();
            }
        });

        service.shutdown();

    }
}
